package com.axend.aerosense.room.ui.fragment.vipfun;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.axend.aerosense.room.entity.RoomVipSetBean;
import com.axend.aerosense.room.entity.VipFunSaveDataBean;
import com.blankj.utilcode.util.LogUtils;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.ui.BottomTextPickerDialog;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomFragmentWorkRestBinding;

/**
 *
 * 作息异常
 */
public class RoomWorkRestFragment extends BaseVipFunFragment<RoomFragmentWorkRestBinding> {

    private int selectValue;
    private String[] stringArray;

    public static RoomWorkRestFragment getInstance(){
        return new RoomWorkRestFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.room_fragment_work_rest;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onChange(CompoundButton buttonView, boolean isChecked) {
        super.onChange(buttonView, isChecked);
    }

    @Override
    protected void saveSettings() {
        super.saveSettings();
        VipFunSaveDataBean bean = new VipFunSaveDataBean(
                getRoomUuid(),
                getRoomType(),
                getCurrentActivity().getSwitchState(),
                selectValue,
                0);
        LogUtils.d("作息异常", bean.toString());
        viewModel.saveData(bean, UrlApi.URL.SetWorkAndrest.getUrl());
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        stringArray = getResources().getStringArray(R.array.common_time_list);
        getCurrentActivity().setVipFunTip(R.string.room_regular_lifestyle_tips);
        setTitle(R.string.room_regular_lifestyle);
        init();
    }

    private void init(){
        //报警设置 报警时间
        viewDataBinding.roomAlarmLayout.setOnClickListener(v -> {
            BottomTextPickerDialog fragment = new BottomTextPickerDialog.Builder()
                    .setType(Type.NUMBER)
                    .setWheelItemTextSize(16)
                    .setDefValue(4)
                    .setThemeColor(getResources().getColor(R.color.app_colorAccent))
                    .setIndexCallBack(new OnDateSetListener<Integer>() {
                        @Override
                        public void onDateSet(Fragment timePickerView, Integer sex) {
                            viewDataBinding.roomAlarmTime.setText(stringArray[sex]);
                            selectValue = sex;
                            change();
                        }
                    })
                    .build(stringArray);
            fragment.show(getChildFragmentManager(), BottomTextPickerDialog.class.getSimpleName());
        });

        RoomVipSetBean vipSettings = getVipSettings();
        if (vipSettings != null){
            getCurrentActivity().setSwitchChange(vipSettings.getFunctionSwitch() == 1);
            selectValue = vipSettings.getFunctionRemind();
            if (selectValue < stringArray.length){
                viewDataBinding.roomAlarmTime.setText(stringArray[selectValue]);
            }
        }
    }

    private String getUnit(){
        return " ";
    }
}
